﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class QuanlyNhomNguoiDung : UserControl
    {
        public QuanlyNhomNguoiDung()
        {
            InitializeComponent();
        }
        private void loadData()
        {
            dgvNhomNguoiDung.Rows.Clear();
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var nhomList = from ig in dc.NhomNguoiDungs
                           where ig.deleted == false 
                           select ig;

            int count = 1;
            foreach (_DB.NhomNguoiDung nhom in nhomList)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvNhomNguoiDung);
                row.Cells[0].Value = count++;
                row.Cells[1].Value = nhom.TenNhom;
                row.Cells[2].Value = nhom.NhomNguoiDung_QuyenHans.Count;
                row.Cells[3].Value = nhom.NguoiDungs.Count;
                row.Tag = nhom.NhomNguoiDung_ID;

                dgvNhomNguoiDung.Rows.Add(row);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OtherForms.ThemNhonNguoiDung f = new OtherForms.ThemNhonNguoiDung();
            f.ShowDialog();
            loadData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvNhomNguoiDung.SelectedRows.Count == 0)
            {
                return;
            }
            int id = (int)dgvNhomNguoiDung.SelectedRows[0].Tag;

            OtherForms.ThemNhonNguoiDung f = new OtherForms.ThemNhonNguoiDung(id, 1);
            f.ShowDialog();
            loadData();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvNhomNguoiDung.SelectedRows.Count == 0)
                return;
            if (MessageBox.Show("Chắc chắn xóa?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            foreach (DataGridViewRow r in dgvNhomNguoiDung.SelectedRows)
            {
                _DB.NhomNguoiDung u = dc.NhomNguoiDungs.Single(x => x.NhomNguoiDung_ID == (int) r.Tag);
                u.deleted = true;
            }

            dc.SubmitChanges();
            loadData();
        }

        private void QuanlyGroupUser_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void dgvNhomNguoiDung_DoubleClick(object sender, EventArgs e)
        {
            if (dgvNhomNguoiDung.SelectedRows.Count == 0)
                return;

            int id = (int)dgvNhomNguoiDung.SelectedRows[0].Tag;
            OtherForms.ThemNhonNguoiDung f = new OtherForms.ThemNhonNguoiDung(id, 0);
            f.Show();
        }
    }
}
